﻿using System;
using BaseTerminalProtocol.Authorizations;
using Helpers;
using System.Xml;
using System.Threading;

namespace BaseTerminalProtocol.TerminalActions.Agents
{
    public class TransferFund
    {
        
        //Request
        /// <summary>
        /// Requests the transfer fund to agent.
        /// </summary>
        /// <param name="authorization">The authorization.</param>
        /// <param name="targetAgent">The target agent.</param>
        /// <param name="paymentID">The payment ID.</param>
        /// <param name="toAgent">To agent.</param>
        /// <param name="amount">The amount.</param>
        /// <returns>
        /// Xml request
        /// </returns>
        public  XmlDocument RequestTransferFundToAgent(Authorization authorization,string targetAgent,string paymentID,string toAgent,double amount)
        {
            XmlDocument doc = XMLGate.Request_INIT(authorization);
            XmlElement xmlRoot = doc.DocumentElement;


            XmlElement xmlAgents = XMLHelper.newElement(doc, "agents");
            XmlElement xmlTransferFunds = XMLHelper.newElement(doc, "transferFunds");
            XmlElement xmlTargetAgent = XMLHelper.newElement(doc, "target-agent", targetAgent);

            XmlElement xmlPayment = XMLHelper.newElement(doc, "payment");
            xmlPayment.Attributes.Append(XMLHelper.newAttribute(doc, "id", paymentID));
            xmlPayment.Attributes.Append(XMLHelper.newAttribute(doc, "agent", toAgent));
            xmlPayment.Attributes.Append(XMLHelper.newAttribute(doc, "enroll", amount.ToString() + ".00"));
            xmlPayment.Attributes.Append(XMLHelper.newAttribute(doc, "comment","so tien [" + amount + "]"));

            xmlTransferFunds.AppendChild(xmlTargetAgent);
            xmlTransferFunds.AppendChild(xmlPayment);

            xmlAgents.AppendChild(xmlTransferFunds);
            if (xmlRoot != null) xmlRoot.AppendChild(xmlAgents);

            return doc;
        }

        //Respones
        /// <summary>
        /// Responses the transfer fund to agent.
        /// </summary>
        /// <param name="paymentID">The payment ID.</param>
        /// <param name="toAgent">To agent.</param>
        /// <param name="amount">The amount.</param>
        /// <returns>
        /// Xml Response
        /// </returns>
        public  XMLGate.GateResponse ResponseTransferFundToAgent(string targetAgent,string paymentID, string toAgent, double amount)
        {
            try
            {
                var authorization = new Authorization();
                Tools.Logger("ResponseTransferFundToAgent start ----------------------------------------", "info", "ResponseTransferFundToAgent");
                string request = "";

                var requestTransferToAgent = RequestTransferFundToAgent(authorization, targetAgent, paymentID, toAgent, amount);
                Tools.Logger("requestTransferToAgent: " + requestTransferToAgent.InnerXml, "info", "TransferToAgent");

                var requestSend = XMLGate.Request_SEND(requestTransferToAgent,authorization.ServerUrl, out request);
                Tools.Logger("requestTransferToAgent: " + requestSend.InnerXml, "info", "TransferToAgent");

                var response = new XMLGate.GateResponse(requestSend);
                Tools.Logger("Request: " + request, "info", "TransferCommission");
                Tools.Logger("Response: " + response.ResponseData, "info", "ResponseTransferFundToAgent");
                return response;
            }
            catch (Exception ex)
            {
                Tools.Logger("TransferCommission : " + ex.Message, "error", "ResponseTransferFundToAgent");
                Tools.Logger("TransferCommission : " + ex.StackTrace, "error", "ResponseTransferFundToAgent");
                Tools.Logger("TransferCommission : " + ex.InnerException, "error", "ResponseTransferFundToAgent");
            }
            return null;
        }
    }
}
